<?php

namespace App\Http\Logic\Admin;

use App;
use App\Common\Status;
use App\Models\RecruitPositionCategory;
use App\Helper\Helper;
use Illuminate\Support\Facades\DB;

class PositionCategoryLogic
{
    /**
     * @return mixed
     */
    public static function getCategoryList($params)
    {
        $where['status'] = Status::REGULAR;
        if (isset($params['id']) && $params['id']) {
            $ids = DB::table('gz_users as a')
                ->join('recruit_notes_intention as b', function ($join) {
                    $join->on("a.mobile", "=", "b.mobile");
                    $join->where("b.status", '=', Status::REGULAR);
                    $join->where("b.is_deleted", '=', Status::NOT_DELETE);
                })
                ->where("a.id", $params['id'])
                ->select(["b.position_category_id"])
                ->get();
            if ($ids->isNotEmpty()) {
                $cids = $ids->pluck("position_category_id")->toArray();
                return RecruitPositionCategory::where($where)
                    ->whereIn("id", $cids)
                    ->orderBy("created_at", "desc")->get();
            }

        }

        if ($params['pid'] == 1) {
            $where[] = ['pid', '=', 0];
        } elseif ($params['pid'] == 2) {
            $where[] = ['pid', '<>', 0];
        }
        return RecruitPositionCategory::where($where)->orderBy("created_at", "desc")->get();
    }

    /**
     * @param $params
     * @return mixed
     */
    public static function createCategory($params)
    {
        if ($params['id']) {
            $params['updated_at'] = time();
            return RecruitPositionCategory::where('id', $params['id'])->update($params);
        }

        $where['status']      = Status::REGULAR;
        $params['created_at'] = time();
        $params['creator_id'] = Helper::adminById();
        return RecruitPositionCategory::insertGetId($params);
    }


    /**
     * @param $id
     * @return mixed
     */
    public static function getCategoryDtl($id)
    {
        return RecruitPositionCategory::select(["id", "pid", "name"])->find($id);
    }


    /**
     * @param $id
     * @return mixed
     */
    public static function delCategory($id)
    {
        $c = RecruitPositionCategory::where('id', $id)->first();
        if ($c->pid == 0) {
            RecruitPositionCategory::where('id', $id)->update(
                [
                    'status'     => Status::NOT_DELETE,
                    'is_deleted' => Status::DELETED,
                    'deleted_at' => time()
                ]
            );
            return RecruitPositionCategory::where('pid', $id)->update(
                [
                    'status'     => Status::NOT_DELETE,
                    'is_deleted' => Status::DELETED,
                    'deleted_at' => time()
                ]
            );
        }
        return RecruitPositionCategory::where('id', $id)->update(['status' => 0, 'is_deleted' => 1, 'deleted_at' => time()]);
    }
}
